<template>
  <div class="book-detail">
    <div class="book-info">
      <dl>
        <dt>
          <img :src="bookInfo.coverUrl" v-default-img="defaultImg" />
        </dt>
        <dd>
          <h3 class="bookTitle" v-text="bookInfo.bookTitle"></h3>
          <p class="ellipsis authorName" v-text="bookInfo.authorName"></p>
          <p class="bookTxt">
            <span
              v-if="bookInfo.category"
              v-text="bookInfo.category.name"
            ></span>
            <span>{{ bookInfo.writingProcess | writingProcess }}</span>
            <span v-text="bookInfo.wordCount + ' Words'"></span>
          </p>
          <button class="preview" @click="toReader(1)">Read</button>
        </dd>
      </dl>
      <div
        class="bookIntro"
        v-if="bookInfo.bookIntro && bookInfo.bookIntro.length > 0"
      >
        <p :class="isIntroduce ? 'intro-on' : 'intro-off'">
          {{ bookInfo.bookIntro }}
          <mt-button
            v-if="isUnfoldOrFoldBtn"
            @click="isIntroduce = !isIntroduce"
          >
            <template v-if="isIntroduce">
              <svg-icon icon-class="fold"></svg-icon>
              Fold
            </template>
            <template v-else>
              <svg-icon icon-class="unfold"></svg-icon>
              Unfold
            </template>
          </mt-button>
        </p>
      </div>
    </div>
    <main>
      <h2>The book</h2>
      <article>
        <h6
          class="chapterName"
          @click="toReader(1)"
          v-text="bookInfo.firstChapterName"
        ></h6>
        <p class="chapterContent" v-html="bookInfo.firstChapterContent"></p>
        <button @click="toReader(2)" class="preview">Read On</button>
      </article>
      <h2>Related recommendati</h2>
      <ul class="recommend-list">
        <Cover
          v-for="item of recommendList"
          :key="item.contentId"
          :item="item"
        />
      </ul>
    </main>
  </div>
</template>

<script>
import axios from "api/index";
import api from "api/global_variable";
import Cover from "components/common/Cover/Index";
export default {
  data() {
    return {
      bookId: this.$route.params.id, //书籍ID
      defaultImg: require("@/assets/images/default-cover.png"),
      bookInfo: {}, //书籍信息
      isIntroduce: false, // 是否展开简介
      isUnfoldOrFoldBtn: false, // 是否显示展开/隐藏按钮
      recommendList: [], //推荐书籍列表
    };
  },
  filters: {
    // 过滤写作进程
    writingProcess(code) {
      let list = {
        0: "Serial",
        1: "End",
        2: "New",
      };
      return list[code];
    },
  },
  components: {
    Cover,
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    this.getinfo();
  },
  methods: {
    async getinfo() {
      try {
        const baseUrl = api.apiUrl;
        let url = baseUrl + `/book/info/${this.bookId}`;
        let res = await axios.get(url);
        if (res.code === 1) {
          this.bookInfo = res.source;
          if (this.bookInfo.bookIntro.length > 100) {
            this.isUnfoldOrFoldBtn = true;
          } else {
            this.isIntroduce = true;
          }
          this.recommend(this.bookId, res.source.categoryId1);
          this.sendLog(res.source);
        }
      } catch (e) {
        console.log(e);
      }
    },
    // 发送日志
    async sendLog(val) {
      let opts = {
        logType: 1, //日志类型：1.电子书，2.AI听书，3.有声小说
        isTitlePage: 1, //是否扉页：1.是，0.否
        bookId: this.bookId, //书籍id
        bookName: val.bookTitle, //书籍名称
        authorName: val.authorName, //作者名称
        cpId: val.channelId, //cp方id
        preference: val.site, //书籍男女：1.男，2.女
        categoryIdOne: val.categoryId1, //书籍分类一级id
        categoryIdTwo: val.categoryId2, //书籍分类二级id
        isFreeBook: val.isFree, //是否免费书籍：1.是，0.否
      };
      await this.$api.logger.book_read_analysis(opts);
    },
    // 查询的书籍列表
    async recommend(bookId, categoryId) {
      try {
        let params = {
          bookId,
          categoryId,
        };
        let res = await this.$api.book.recommend(params);
        if (res.code === 1) {
          this.recommendList = res.source;
        }
      } catch (e) {
        console.log(e);
      }
    },
    // 跳转阅读器
    toReader(sort) {
      if (sort >= 2) {
        let map;
        if (this.utils.seekStore("readingLog")) {
          map = new Map(this.utils.getStore("readingLog"));
          let maxSort = map.has(this.bookId)
            ? Math.max(map.get(this.bookId), sort)
            : sort;
          map.set(this.bookId, maxSort);
        } else {
          map = new Map();
          map.set(this.bookId, sort);
        }
        this.utils.setStore("readingLog", Array.from(map.entries()));
      }
      this.$router.push({
        name: "reader",
        params: { id: this.bookId }, //书籍id
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.book-detail {
  padding: 40px;
  background: #f5f5f5;
  .book-info {
    width: 100%;
    // min-height: 496px;
    height: auto;
    margin-bottom: 40px;
    box-sizing: border-box;
    padding: 32px;
    background: #fff;
    border-radius: 16px;
    > dl {
      width: 100%;
      height: 320px;
      display: flex;
      justify-content: space-between;
      dt {
        margin-right: 23px;
        img {
          width: 240px;
          height: 320px;
          box-shadow: 0px 1px 6px 1px rgba(0, 0, 0, 0.2);
          border-radius: 16px;
        }
      }
      dd {
        width: 336px;
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        .bookTitle {
          line-height: 36px;
          font-size: 28px;
          font-family: PingFangSC-Semibold, PingFang SC;
        }
        p {
          color: #999999;
          line-height: 28px;
          &.authorName {
          }
          &.bookTxt {
            span {
              &:not(:first-child)::before {
                content: "/";
                padding: 0 10px;
              }
            }
          }
        }
      }
    }
    .bookIntro {
      white-space: pre-wrap;
      > p {
        margin-top: 20px;
        line-height: 28px;
        position: relative;
        color: #666666;
        .mint-button {
          height: 28px;
          background: none;
          border-width: 0;
          box-shadow: none;
          color: #e86472;
          z-index: 9;
          display: flex;
          align-items: center;
        }
        &.intro-off {
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 4;
          overflow: hidden;
          max-height: 84px;
          &::after {
            content: "...";
            position: absolute;
            bottom: 0;
            right: 0;
            padding-right: 200px;
            padding-left: 4px;
            background: linear-gradient(to right, transparent, #fff 0%);
          }
          .mint-button {
            position: absolute;
            bottom: 0;
            right: 0;
          }
        }
        &.intro-on {
          .mint-button {
            flex: 1;
            margin-left: auto;
            text-align: right;
          }
        }
      }
    }
  }
  main {
    width: 100%;
    height: auto;
    box-sizing: border-box;
    padding: 32px;
    background: #fff;
    border-radius: 16px;
    h2 {
      font-size: 34px;
      line-height: 48px;
      margin-bottom: 23px;
      &::before {
        content: "";
        width: 8px;
        height: 30px;
        border-radius: 0px 4px 4px 0px;
        display: inline-block;
        margin-right: 10px;
        background: linear-gradient(147deg, #f56580 0%, #bd4ff0 100%);
      }
    }
    article {
      .chapterName {
        line-height: 28px;
        margin-bottom: 20px;
      }
      .chapterContent {
        white-space: pre-wrap;
        color: #666666;
        margin-bottom: 22px;
        line-height: 28px;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      .preview {
        margin: auto;
        margin-bottom: 46px;
      }
    }
    .recommend-list {
      display: flex;
      overflow-y: hidden;
      overflow-x: auto;
      padding: 8px 0;
      &::-webkit-scrollbar {
        width: 0;
        height: 0;
      }
    }
  }
  .preview {
    width: 160px;
    height: 50px;
    display: block;
    font-size: 22px;
    font-weight: bold;
    font-family: PingFang-SC-Bold, PingFang-SC;
    color: #fff;
    background: #e86472;
    border-width: 0;
    border-radius: 6px;
  }
}
</style>
